package com.gitee.feizns.quickstart.web.spring.boot.autoconfigure;

import com.gitee.feizns.quickstart.spring.boot.autoconfigure.SpringBoots;
import com.gitee.feizns.quickstart.upload.DiskFileStorage;
import com.gitee.feizns.quickstart.upload.FileStorage;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

import java.io.File;

/**
 * @author feizns
 * @since 2025/8/14
 */
@Configuration
public class QuickstartUploadAutoConfiguration {

    /**
     * 文件存储
     * @return {@link FileStorage }
     */
    @Lazy
    @Bean
    @ConditionalOnMissingBean
    public FileStorage fileStorage() {
        return new DiskFileStorage(new File(getJarExecuteDirectory() + "/static", "static").getAbsolutePath());
    }

    /**
     * 上传控制器
     * @return {@link UploadController}
     */
    @Bean
    @ConditionalOnMissingBean
    public UploadController uploadController() {
        return new UploadController();
    }

    /**
     * 获取 jar 执行目录
     * @return {@link File}
     */
    public static File getJarExecuteDirectory() {
        return new ApplicationHome(SpringBoots.app().getMainApplicationClass()).getSource().getParentFile();
    }

}
